package com.youyiwen.Service.impl;

import com.youyiwen.Bean.Comment;
import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.CommentMapper;
import com.youyiwen.Mapper.ReplyMapper;
import com.youyiwen.Mapper.UserMapper;
import com.youyiwen.Service.CommentService;
import com.youyiwen.Service.ReplyService;
import com.youyiwen.dto.CommentDTO;
import com.youyiwen.dto.ReplyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhaoyang
 * @Date: 2021/03/27
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ReplyService replyService;

    @Autowired
    private ReplyMapper replyMapper;

    @Override
    public void insertComment(Comment comment)  {
        commentMapper.insertComment(comment);
    }

    @Override
    public List<CommentDTO> commentDTOList(String id) {
        //comment信息放入commentdtoList中去
        List<Comment> commentList = commentMapper.selectComments(id);
        List<CommentDTO> comments = new ArrayList<>();



        for (Comment comment : commentList){
            //设置属性
            CommentDTO commentDTO = new CommentDTO();

            User user = userMapper.selectUserByName(comment.getCommentator());
            String commentId = comment.getId();
            commentDTO.setId(commentId);
            commentDTO.setGmtCreate(comment.getGmtCreate());
            commentDTO.setUser(user);
            commentDTO.setParentId(comment.getParentId());
            commentDTO.setContent(comment.getContent());
            commentDTO.setType(comment.getType());
            //获取该评论的回复
            List<ReplyDTO> replyDTOList = replyService.selectReplyListByPId(commentId);
            Integer count = replyMapper.selectCount(commentId);
            commentDTO.setReplyCount(count);
            commentDTO.setReplies(replyDTOList);
            comments.add(commentDTO);
        }
        return comments;
    }

    @Override
    public void delectCommentById(String id) {
        commentMapper.delectCommentById(id);
    }

}
